#!/bin/sh -e

# Script to create header files and links to configure
# U-Boot for a specific board.
#
# Parameters:  Target  Architecture  CPU  Board [VENDOR] [SOC]
#
# (C) 2002-2013 DENX Software Engineering, Wolfgang Denk <wd@denx.de>
#
# SPDX-License-Identifier:	GPL-2.0+
#

APPEND=no	# Default: Create new config file
BOARD_NAME=""	# Name to print in make output
TARGETS=""

arch=""
cpu=""
board=""
vendor=""
soc=""
options=""

if [ \( $# -eq 2 \) -a \( "$1" = "-A" \) ] ; then
	# Automatic mode
	line=`awk '($0 !~ /^#/ && $7 ~ /^'"$2"'$/) { print $1, $2, $3, $4, $5, $6, $7, $8 }' $srctree/boards.cfg`
	if [ -z "$line" ] ; then
		echo "make: *** No rule to make target \`$2_config'.  Stop." >&2
		exit 1
	fi

	set ${line}
	# add default board name if needed
	[ $# = 3 ] && set ${line} ${1}
fi

while [ $# -gt 0 ] ; do
	case "$1" in
	--) shift ; break ;;
	-a) shift ; APPEND=yes ;;
	-n) shift ; BOARD_NAME="${7%_config}" ; shift ;;
	-t) shift ; TARGETS="`echo $1 | sed 's:_: :g'` ${TARGETS}" ; shift ;;
	*)  break ;;
	esac
done

[ $# -lt 7 ] && exit 1
[ $# -gt 8 ] && exit 1

# Strip all options and/or _config suffixes
CONFIG_NAME="${7%_config}"

[ "${BOARD_NAME}" ] || BOARD_NAME="${7%_config}"

arch="$2"
cpu=`echo $3 | awk 'BEGIN {FS = ":"} ; {print $1}'`
spl_cpu=`echo $3 | awk 'BEGIN {FS = ":"} ; {print $2}'`

if [ "$cpu" = "-" ] ; then
	cpu=
fi

[ "$6" != "-" ] && board="$6"
[ "$5" != "-" ] && vendor="$5"
[ "$4" != "-" ] && soc="$4"
[ $# -gt 7 ] && [ "$8" != "-" ] && {
	# check if we have a board config name in the options field
	# the options field mave have a board config name and a list
	# of options, both separated by a colon (':'); the options are
	# separated by commas (',').
	#
	# Check for board name
	tmp="${8%:*}"
	if [ "$tmp" ] ; then
		CONFIG_NAME="$tmp"
	fi
	# Check if we only have a colon...
	if [ "${tmp}" != "$8" ] ; then
		options=${8#*:}
		TARGETS="`echo ${options} | sed 's:,: :g'` ${TARGETS}"
	fi
}

if [ "${ARCH}" -a "${ARCH}" != "${arch}" ]; then
	echo "Failed: \$ARCH=${ARCH}, should be '${arch}' for ${BOARD_NAME}" 1>&2
	exit 1
fi

#
# Test above needed aarch64, now we need arm
#
if [ "${arch}" = "aarch64" ]; then
	arch="arm"
fi

if [ "$options" ] ; then
	echo "Configuring for ${BOARD_NAME} - Board: ${CONFIG_NAME}, Options: ${options}"
else
	echo "Configuring for ${BOARD_NAME} board..."
fi

#
# Create link to architecture specific headers
#
if [ -n "$KBUILD_SRC" ] ; then
	mkdir -p ${objtree}/include
	LNPREFIX=${srctree}/arch/${arch}/include/asm/
	cd ${objtree}/include
	mkdir -p asm
else
	cd arch/${arch}/include
fi

rm -f asm/arch

if [ "${soc}" ] ; then
	ln -s ${LNPREFIX}arch-${soc} asm/arch
elif [ "${cpu}" ] ; then
	ln -s ${LNPREFIX}arch-${cpu} asm/arch
fi

if [ -z "$KBUILD_SRC" ] ; then
	cd ${srctree}/include
fi

#
# Create include file for Make
#
( echo "ARCH   = ${arch}"
    if [ ! -z "$spl_cpu" ] ; then
	echo 'ifeq ($(CONFIG_SPL_BUILD),y)'
	echo "CPU    = ${spl_cpu}"
	echo "else"
	echo "CPU    = ${cpu}"
	echo "endif"
    else
	echo "CPU    = ${cpu}"
    fi
    echo "BOARD  = ${board}"

    [ "${vendor}" ] && echo "VENDOR = ${vendor}"
    [ "${soc}"    ] && echo "SOC    = ${soc}"
    exit 0 ) > config.mk

# Assign board directory to BOARDIR variable
if [ -z "${vendor}" ] ; then
    BOARDDIR=${board}
else
    BOARDDIR=${vendor}/${board}
fi

#
# Create board specific header file
#
if [ "$APPEND" = "yes" ]	# Append to existing config file
then
	echo >> config.h
else
	> config.h		# Create new config file
fi
echo "/* Automatically generated - do not edit */" >>config.h

for i in ${TARGETS} ; do
	i="`echo ${i} | sed '/=/ {s/=/	/;q; } ; { s/$/	1/; }'`"
	echo "#define CONFIG_${i}" >>config.h ;
done

echo "#define CONFIG_SYS_ARCH  \"${arch}\""  >> config.h
echo "#define CONFIG_SYS_CPU   \"${cpu}\""   >> config.h
echo "#define CONFIG_SYS_BOARD \"${board}\"" >> config.h

if [ "x${SUNXI_MODE}" = "x${OTA_TEST_NAME}" ] ; then
	echo "#define SUNXI_OTA_TEST" >> config.h
fi
[ "${vendor}" ] && echo "#define CONFIG_SYS_VENDOR \"${vendor}\"" >> config.h

[ "${soc}"    ] && echo "#define CONFIG_SYS_SOC    \"${soc}\""    >> config.h

[ "${board}"  ] && echo "#define CONFIG_BOARDDIR board/$BOARDDIR" >> config.h
cat << EOF >> config.h
#include <config_cmd_defaults.h>
#include <config_defaults.h>
#include <configs/${CONFIG_NAME}.h>
#include <asm/config.h>
#include <config_fallbacks.h>
#include <config_uncmd_spl.h>
EOF

exit 0
